package com.lanou.elh.cart.service.impl;

import com.lanou.elh.cart.entity.Item;
import com.lanou.elh.cart.mapper.CartMapper;
import com.lanou.elh.cart.service.CartService;
import com.lanou.elh.cart.web.controller.CartController;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.*;


@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    private static final Logger logger = LoggerFactory.getLogger(CartController.class);

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public void add(Map<String,Object> cart) {
        //获取前台的用户id 商品id 商品数量
        //用户id 作为购物车id
        String userId = cart.get("userId").toString();
        String productId = cart.get("productId").toString();
        String productNum = cart.get("productNum").toString();



        //判断该用户的购物车里 是否存在添加的商品

        Boolean hasItem = stringRedisTemplate.opsForHash().hasKey(userId,productId);
//        System.out.println(hasItem);
        if(hasItem){
            //如果购物车存在该商品
            //获取添加商品购物车里的数量
            String  number =stringRedisTemplate.opsForHash().get(userId, productId).toString();
            System.out.println(number);
            Integer oldNum = Integer.valueOf(productNum);
            Integer newNum = Integer.valueOf(number);
            Integer cartNum = oldNum+newNum;
            stringRedisTemplate.opsForHash().put(userId,productId,cartNum);
        }else {
            //如果购物车不存在该商品 添加进购物车
            stringRedisTemplate.opsForHash().put(userId,productId,productNum);
        }

    }

    @Override
    public List<Item> list(Integer userId) {
        List<Item> itemList = new ArrayList<>();
         //根据用户查看用户购物车
        //查看购物车所有商品的id和数量

        String uid = userId.toString();
        //获取购物车商品所有的商品id
        Set cartPid = stringRedisTemplate.opsForHash().keys(uid);

        Iterator<String> it = cartPid.iterator();
        while (it.hasNext()){
            String pid = it.next();
            String num = stringRedisTemplate.opsForHash().get(uid, pid).toString();
            Long proId = Long.parseLong(pid);
            Item item = cartMapper.findProductByPid(proId);
            itemList.add(item);
        }
        return itemList;
    }

    @Override
    public void del(Map<String, Object> cartOne) {
        String uid = cartOne.get("userId").toString();
        String pid = cartOne.get("productId").toString();
        stringRedisTemplate.opsForHash().delete(uid,pid);
    }

    @Override
    public void editCart(Map<String, Object> editCart) {
        //通过购物车id 商品id 修改商品数量
        String cartId = editCart.get("userId").toString();
        String productId = editCart.get("productId").toString();
        String productNum = editCart.get("productNum").toString();
        stringRedisTemplate.opsForHash().put(cartId,productId,productNum);

    }


    @Override
    public void set() {

//        redisTemplate.opsForHash().put("500", "aa","001");
//        redisTemplate.opsForHash().put("500", "bb","002");
//        redisTemplate.opsForHash().put("500", "cc","003");
//        redisTemplate.opsForHash().put("500", "dd","004");
        stringRedisTemplate.opsForHash().put("500", "ee","006");

        System.out.println("success");

    }



    @Override
    public void show() {
//        logger.info(redisTemplate.opsForValue().get("test").toString());
//        Set keys = redisTemplate.opsForHash().keys("62");
//        System.out.println(keys);
//        String  number =redisTemplate.opsForHash().get("62", "150642571432844").toString();
        Map entries = stringRedisTemplate.opsForHash().entries("500");
        System.out.println(entries);
//        System.out.println("cartNum"+number);
    }


}
